﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Abstract;
using Domain.Entity;
using System.Data;
using System.Text.RegularExpressions;

namespace Domain.Concrete
{
    public class EFPlatformRepository : IPlatformRepository
    {
        public EFDbContext Context { get; set; }

        public EFPlatformRepository()
        {
            Context = EFDbContext.Instance;
        }

        public EFPlatformRepository(EFDbContext context)
        {
            Context = context;
        }

        public IQueryable<Platform> Platforms
        {
            get { return Context.Platforms; }
        }

        public void SavePlatform(Platform platform)
        {
            if (platform.PlatformID == 0)
            {
                Context.Platforms.Add(platform);
            }
            else
            {
                Context.Entry(platform).State = EntityState.Modified;
            }
            Context.SaveChanges();
        }

        public void RemovePlatform(Platform platform)
        {
            var dbPlatform = Platforms.SingleOrDefault(x => x.PlatformID == platform.PlatformID);

            if(dbPlatform != null)
                Context.Platforms.Remove(dbPlatform);

            Context.SaveChanges();
        }

        public Platform FindPlatform(int id)
        {
            return Context.Platforms.FirstOrDefault(x => x.PlatformID == id);
        }

        public List<Platform> SearchPlatform(string search = "")
        {
            List<Platform> platformsFound = new List<Platform>();
            
            foreach (var p in Context.Platforms)
            {
                bool add = true;
                //Searches by name
                if (Regex.IsMatch(p.Name, search))
                {
                    if (platformsFound.Count > 0)
                    {
                        foreach (var pf in platformsFound)
                        {
                            if (p.Name.Equals(pf.Name))
                            {
                                add = false;
                            }
                        }
                        if (add)
                        {
                            platformsFound.Add(p);
                        }
                    }
                    else
                    {
                        platformsFound.Add(p);
                    }
                }
                //Serches by generation
                if (p.Generation.ToString().Equals(search))
                {
                    if (platformsFound.Count > 0)
                    {
                        foreach (var pf in platformsFound)
                        {
                            if (p.Name.Equals(pf.Name))
                            {
                                add = false;
                            }
                        }
                        if (add)
                        {
                            platformsFound.Add(p);
                        }
                    }
                    else
                    {
                        platformsFound.Add(p);
                    }
                }
                //searches by release year
                if (p.YearReleased.ToString().Equals(search))
                {
                    if (platformsFound.Count > 0)
                    {
                        foreach (var pf in platformsFound)
                        {
                            if (p.Name.Equals(pf.Name))
                            {
                                add = false;
                            }
                        }
                        if (add)
                        {
                            platformsFound.Add(p);
                        }
                    }
                    else
                    {
                        platformsFound.Add(p);
                    }
                }
            }

            return platformsFound;
        }
    }
}
